package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(description = "SPU管理接口")
@RestController // @ResponseBody + @Controller
@RequestMapping("admin/product")
public class SpuManageController {

    @Autowired
    private ManageService manageService;

    // 根据查询条件封装控制器
    // http://api.gmall.com/admin/product/ {page}/{limit}?category3Id=61
    // 如何从参数 SpuInfo 如何获取 category3Id？ 使用对象属性传值
    // 对象属性传值：如果页面提交过来的参数与实体类的参数一致，就会进行传值
    @ApiOperation(value = "根据三级分类id分页查询SPU")
    @GetMapping("{page}/{limit}")
    public Result getSpuInfoPage(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 SpuInfo spuInfo) {
        // 创建一个 page 分页对象
        Page<SpuInfo> spuInfoPage = new Page<>(page, limit);

        // 获取分页数据
        IPage<SpuInfo> supInfoPageList = manageService.getSupInfoPage(spuInfoPage, spuInfo);

        return Result.ok(supInfoPageList);
    }

    // http://api.gmall.com/admin/product/baseSaleAttrList
    @ApiOperation(value = "获取销售属性列表")
    @GetMapping("baseSaleAttrList")
    public Result baseSaleAttrList() {
        // 调用service
        List<BaseSaleAttr> baseSaleAttrList = manageService.getBaseSaleAttrList();

        return Result.ok(baseSaleAttrList);
    }


    @ApiOperation(value = "保存spu")
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo) {
        // 调用服务层的保存方法
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }
}
